﻿using Sidvall.Business;
using System.Collections.Generic;
using System.ComponentModel;

namespace Sidvall.WorkItemManager.Data
{
    public class SensorCollection : System.Collections.ObjectModel.ObservableCollection<Sensor>
    {
        #region Public Members

        public event System.EventHandler<ChildChangedEventArgs> ChildChanged;

        #region Add

        public void Add(string name, string description)
        {
            var item = new Sensor()
            {
                Name = name,
                Description = description,
            };
            Add(item);
        }

        #endregion
        #region AddRange

        public void AddRange(IEnumerable<Sensor> items)
        {
            if (items == null)
                return;
            foreach (var item in items)
                Add(item);
        }

        #endregion
        #region ClearItems

        protected override void ClearItems()
        {
            foreach (var item in this)
                RemoveEventHooks(item);
            base.ClearItems();
        }

        #endregion
        #region InsertItem

        protected override void InsertItem(int index, Sensor item)
        {
            base.InsertItem(index, item);
            AddEventHooks(item);
        }

        #endregion
        #region RemoveItem

        protected override void RemoveItem(int index)
        {
            var item = this[index];
            RemoveEventHooks(item);
            base.RemoveItem(index);
        }

        #endregion
        #region SetItem

        protected override void SetItem(int index, Sensor item)
        {
            var child = this[index];
            if (!ReferenceEquals(child, item))
            {
                RemoveEventHooks(child);
                base.SetItem(index, item);
                AddEventHooks(item);
            }
        }

        #endregion

        #region OnChildChanged

        protected void OnChildChanged(object child, PropertyChangedEventArgs childArgs)
        {
            var args = new ChildChangedEventArgs(child, childArgs);
            OnChildChanged(args);
        }
        protected virtual void OnChildChanged(ChildChangedEventArgs args)
        {
            if (this.ChildChanged != null)
                this.ChildChanged.Invoke(this, args);
        }

        #endregion

        #endregion
        #region Private Members

        #region AddEventHooks

        private void AddEventHooks(Sensor item)
        {
            if (item == null)
                return;
            item.PropertyChanged += OnChildChanged;
        }

        #endregion
        #region RemoveEventHooks

        private void RemoveEventHooks(Sensor item)
        {
            if (item == null)
                return;
            item.PropertyChanged -= OnChildChanged;
        }

        #endregion

        #endregion
    }
}
